﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace 提交管理平台V1
{
    public partial class frmFieldsSettings : Form
    {
        string strSettings = string.Empty;

        string strTableName = string.Empty;

        public bool bCancel = false;

        /// <summary>
        /// 模板模式，允许不选择序号列
        /// </summary>
        bool bTemplateMode = false;

        SqlTools sqt;

        public frmFieldsSettings(string strTmpSettings,string strTmpTableName,bool bTmpTemplateMode)
        {
            InitializeComponent();
            strSettings = strTmpSettings;
            strTableName = strTmpTableName;
            bTemplateMode = bTmpTemplateMode;
        }

        private void frmFieldsSettings_Load(object sender, EventArgs e)
        {
            sqt = new SqlTools(GeneralTools.GetConnectionString(提交管理平台V1.Properties.Settings.Default.Host, "DefualtDataBase"));
            iniFieldsList();
            ReadSetting();
        }

        private void iniFieldsList()
        {
            SqlTools.DataStruct ds = sqt.GetTableStruct (strTableName );
            for (int i = 0; i < ds.Fields.Count; i++)
            {
                lstDBFields.Items.Add(ds.Fields[i].FieldName);
            }
            lstDBFields.Items.Add("客户名称");
            if (bTemplateMode)
            {
                lstDBFields.Items.Add("*型号规格");
                lstDBFields.Items.Add("*空列");
            }
        }

        private void ReadSetting()
        {
            if (strSettings.Trim().Equals(string.Empty))
            {
                if (bTemplateMode == false)
                {
                    //如果没有找到用户设置，且当前为非模板模式，则自动选择序号列
                    lstDBFields.Items.Remove("序号");
                    lstSelectedFields.Items.Add("序号");
                }
            }
            else
            {
                string[] strSelectedFields = strSettings.Split('|');
                for (int i = 0; i < strSelectedFields.Length; i++)
                {
                    if (strSelectedFields[i].Trim().Equals(string.Empty))
                    {
                        continue;
                    }
                    lstDBFields.Items.Remove(strSelectedFields[i].Trim());
                    lstSelectedFields.Items.Add(strSelectedFields[i].Trim());
                }
            }
        }

        private void lstDBFields_DoubleClick(object sender, EventArgs e)
        {
            if (lstDBFields.SelectedItem != null)
            {
                object tmpobj = lstDBFields.SelectedItem;
                lstDBFields.Items.Remove(tmpobj);
                lstSelectedFields.Items.Add(tmpobj);
            }
        }

        private void lstSelectedFields_DoubleClick(object sender, EventArgs e)
        {
            if (lstSelectedFields.SelectedItem != null)
            {
                if ((!bTemplateMode) && lstSelectedFields.SelectedIndex == 0)
                {
                    return;
                }
                object tmpobj = lstSelectedFields.SelectedItem;
                lstSelectedFields.Items.Remove(tmpobj);
                lstDBFields.Items.Add(tmpobj);
            }
        }

        private void tsmiSelectAll_Click(object sender, EventArgs e)
        {
            if (lstDBFields.Items.Count > 0)
            {
                foreach (object tmpobj in lstDBFields .Items )
                {
                    lstSelectedFields.Items.Add(tmpobj);
                }
                lstDBFields.Items.Clear();
            }
        }

        private void tsmiUnselectAll_Click(object sender, EventArgs e)
        {
            if (lstSelectedFields.Items.Count > 1)
            {
                foreach (object tmpobj in lstSelectedFields.Items  )
                {
                    if ((!bTemplateMode) &&tmpobj.Equals("序号"))
                    {
                        continue;
                    }
                    lstDBFields.Items.Add(tmpobj);
                }
                if (!bTemplateMode)
                {
                    lstSelectedFields.Items.Clear();
                    lstSelectedFields.Items.Add("序号");
                }
            }
        }

        private void btnUp_Click(object sender, EventArgs e)
        {
            if (lstSelectedFields.SelectedItem != null && lstSelectedFields.SelectedIndex > 0)
            {
                if ((!bTemplateMode) && lstSelectedFields.SelectedIndex == 1)
                {
                    return;
                }
                object tmpobj = lstSelectedFields.SelectedItem;
                lstSelectedFields.Items[lstSelectedFields.SelectedIndex] = lstSelectedFields.Items[lstSelectedFields.SelectedIndex - 1];
                lstSelectedFields.Items[lstSelectedFields.SelectedIndex - 1] = tmpobj;
                lstSelectedFields.SelectedIndex -= 1;
            }
        }

        private void btnDown_Click(object sender, EventArgs e)
        {
            if (lstSelectedFields.SelectedItem != null && lstSelectedFields.SelectedIndex <lstSelectedFields .Items.Count -1)
            {
                if ((!bTemplateMode) && lstSelectedFields.SelectedIndex == 0)
                {
                    return;
                }
                object tmpobj = lstSelectedFields.SelectedItem;
                lstSelectedFields.Items[lstSelectedFields.SelectedIndex] = lstSelectedFields.Items[lstSelectedFields.SelectedIndex + 1];
                lstSelectedFields.Items[lstSelectedFields.SelectedIndex + 1] = tmpobj;
                lstSelectedFields.SelectedIndex += 1;
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            bCancel = true;
            this.Close();
        }

        private void btnDefualt_Click(object sender, EventArgs e)
        {
            lstDBFields.Items.Clear();
            lstSelectedFields.Items.Clear();
            iniFieldsList();
            string[] strDefualt = new string[] {"序号","提交时间","合同号","质量等级","型号","尺寸","特性","电压","容量","批号","芯片号","提交数量","合同数量","交货期","生产日期","淘汰率","筛选数量","执行标准","备注","已开票","开票日期","已备资料","备资料日期"};
            for (int i = 0; i < strDefualt.Length; i++)
            {
                lstDBFields.Items.Remove(strDefualt[i]);
                lstSelectedFields.Items.Add(strDefualt[i]);
            }
        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            strSettings = string.Empty;
            for (int i = 0; i < lstSelectedFields.Items.Count; i++)
            {
                strSettings += lstSelectedFields.Items[i].ToString();
                if (i < lstSelectedFields.Items.Count - 1)
                {
                    strSettings += "|";
                }
            }
            this.Close();
        }

        /// <summary>
        /// 显示字段设置窗口
        /// </summary>
        /// <param name="strTmpSettings">原字段设置</param>
        /// <param name="bTmpTemplateMode">模板模式，允许不选择序号列</param>
        /// <returns>新字段设置</returns>
        public static DialogResult Show(ref string strTmpSettings,string strTmpTableName,bool bTmpTemplateMode)
        {
            frmFieldsSettings newFrmFieldSettings = new frmFieldsSettings(strTmpSettings,strTmpTableName ,bTmpTemplateMode );
            newFrmFieldSettings.StartPosition = FormStartPosition.CenterParent;
            newFrmFieldSettings.ShowDialog();
            if (newFrmFieldSettings.bCancel)
            {
                return DialogResult.Cancel;
            }
            else
            {
                strTmpSettings = newFrmFieldSettings.strSettings;
                return DialogResult.OK;
            }
        }
    }
}